package com.phonegap.extensions;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.webkit.WebView;

public class CommandIncludeExecutor implements ICommandExecutor {

	private Command command;
	private WebView appView;
	
	public CommandIncludeExecutor() {}
	
	public void setCommand(Command command) {
		this.command = command;
	}
	
	public void setAppView(WebView appView) {
		this.appView = appView;
	}
	
	public String process() {
		String html = ""; 
		
		try {
			
			
			String arquivoHtml = command.getProperty("file");
			
			
			android.content.res.AssetManager mgr = appView.getContext().getAssets();
			
			java.io.InputStream in = mgr.open("www/" + arquivoHtml); 
			StringBuffer inLine = new StringBuffer(); 
						
			InputStreamReader isr = new InputStreamReader(in); 
			
			String text;
			BufferedReader inRd = new BufferedReader(isr); 
			
			        
			while ((text = inRd.readLine()) != null) { 
				inLine.append(text); 
				inLine.append("\n"); 
					
			} 
			in.close(); 
			html =  inLine.toString();

		} catch (Exception e) {
			html = "<font color='red'>" + e.toString() + "</font>";
		}		

		
		return html;
	}		
}